package com.jiaquyun.jcps.ui.mine

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.base.ViewModeActivity
import com.jiaquyun.jcps.entity.OrderGoodsListEntity
import com.jiaquyun.jcps.entity.OrderTodayDetailsEntity
import com.jiaquyun.jcps.mode.vm.TodayViewModel
import com.lemlin.network.HttpServiceStatus
import kotlinx.android.synthetic.main.activity_today_order_details.*
import java.lang.StringBuilder

class TodayOrderDetailsActivity : ViewModeActivity<TodayViewModel>() {

    companion object {
        private const val EXTRA_KEY_ID = "EXTRA_KEY_ID"
        fun open(activity: Activity, orderId: String) {
            val intent = Intent(activity, TodayOrderDetailsActivity::class.java)
            intent.putExtra(EXTRA_KEY_ID, orderId)
            activity.startActivity(intent)
        }
    }

    private val orderGoodsAdapter =
        object :
            BaseQuickAdapter<OrderGoodsListEntity, BaseViewHolder>(R.layout.item_order_details) {
            override fun convert(holder: BaseViewHolder, item: OrderGoodsListEntity) {
                holder.setText(
                    R.id.tvOrderGoodsNameNum,
                    StringBuilder().append(item.goods_name).append("\tx").append(item.goods_num)
                )
                    .setText(
                        R.id.tvOrderGoodsPrice,
                        StringBuilder().append("￥").append(item.final_price)
                    )
            }
        }

    override fun viewModel(): TodayViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_today_order_details

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.colorWhite, view = flOrderDetailsBar)
    }


    override fun initView() {

        ivOrderDetailsBack.setOnClickListener {
            finish()
        }

        rvOrderDetailsBody.adapter = orderGoodsAdapter
    }

    override fun initData() {
        intent?.getStringExtra(EXTRA_KEY_ID)?.let {
            viewModel.orderDetailsToday(it)
        }
    }

    override fun initViewMode() {
        viewModel.orderTodayDetailsLiveData.observe(this, Observer { response ->
            if (response.statusCode == HttpServiceStatus.Status.FAILURE)
                toast(response.statusTip)
            if (response.statusCode == HttpServiceStatus.Status.SUCCESS) {
                response.httpData?.data?.let {
                    fillData(it)
                }
            }
        })
    }

    private fun fillData(orderDetails: OrderTodayDetailsEntity) {
        tvOrderDetailsTime.text =
            TimeUtils.millis2String((orderDetails.createtime * 1000), ("yyyy-MM-dd HH:mm"))
        tvOrderDetailsAddressStart.text = orderDetails.start_address
        tvOrderDetailsAddressEnd.text = orderDetails.end_address
        tvOrderDetailsRemarks.text = orderDetails.user_note
        tvOrderDetailsSn.text = orderDetails.order_sn
        tvOrderDetailsActualPrice.text =
            StringBuilder().append("￥").append(orderDetails.order_amount)
        orderGoodsAdapter.setList(orderDetails.order_goods)
    }
}